#!/usr/bin/python
# find rest stops in a gpx file

import sys
import datetime
import math

try:
  from xml.etree import ElementTree # for Python 2.5 users
except:
  from elementtree import ElementTree

if sys.version_info < (2, 4):
    raise "You probably need python 2.5 or greater"

class TP(object):
  def __init__(self, lat, lon, time):
    self.lat = lat
    self.lon = lon
    self.time = time

  def distanceFrom(self, other):
    la = self.lat - other.lat
    lo = self.lon - other.lon
    return int(math.sqrt((la * la) + (lo * lo)) * 100000000)

def main():
  filename = "2008-10-04-WhiskeyTown.gpx"
  tree = ElementTree.parse(filename)
  root = tree.getroot()
  namespace = "http://www.topografix.com/GPX/1/0"
  trkpts = root.findall('.//{%s}trkpt' % namespace)
  points = []
  last_point = None
  last_mark = "S"
  bs = None
  axis = datetime.date(1900,1,1)
  for trkpt in trkpts:
    lat = float(trkpt.attrib.get("lat"))
    lon = float(trkpt.attrib.get("lon"))
    t = trkpt.find('.//{%s}time' % namespace).text
    t = datetime.datetime(int(t[0:4]), int(t[5:7]), int(t[8:10]),
                          int(t[11:13]), int(t[14:16]), int(t[17:19]))
    t -= datetime.timedelta(seconds = 60 * 60 * 8)

    this_point = TP(lat, lon, t)
    if last_point is not None:
      diff = this_point.time - last_point.time
      dist = this_point.distanceFrom(last_point)
      speed = dist / diff.seconds
      for x in range(diff.seconds):
        legend = axis.isoformat()
        print "%s,%s" % (legend, min(speed, 5000))
        axis += datetime.timedelta(days=1)
      # print "%s : %5f : %r" % (last_point.time, dist, diff.seconds / 60)
    last_point = this_point


    x = """
    elif this_point.time - last_point.time > datetime.timedelta(seconds=30):
      dist = this_point.distanceFrom(last_point)
      diff = this_point.time - last_point.time
      print "%10d %s" % (diff.seconds, int(dist * 10000000))
      mark = dist > 0.0003
      if False and mark != last_mark:
        if bs:
          delt = this_point.time - bs
          delt = delt.seconds / 60
          if mark:
            print "%s : Broke for %r" % (this_point.time, delt)
          # print "%s : %0.7f %s" % (this_point.time, dist, mark)
          if not mark and bs:
            print "%s : Rode for %r" % (this_point.time, delt)
        bs = this_point.time

      last_point = this_point
      last_mark = mark
"""

if __name__ == '__main__':
  main()

x = """
2008-10-04 21:23:26 : 0.0011461 *
2008-10-04 21:34:28 : 0.0001156
2008-10-04 21:36:13 : 0.0000480
2008-10-04 21:36:44 : 0.0000215
2008-10-04 21:37:29 : 0.0001517
2008-10-04 21:38:03 : 0.0006256 *
"""


y = """
2008-10-04 08:48:41 : 0.000030 : 9
2008-10-04 09:07:38 : 0.000064 : 2
2008-10-04 09:09:54 : 0.000021 : 6

2008-10-04 11:11:17 : 0.000021 : 7
2008-10-04 11:18:50 : 0.000077 : 8

2008-10-04 13:23:46 : 0.000030 : 10

2008-10-04 14:31:13 : 0.000434 : 7
2008-10-04 14:40:13 : 0.000309 : 14
2008-10-04 14:55:55 : 0.000131 : 6
2008-10-04 15:01:56 : 0.000030 : 3
2008-10-04 15:05:24 : 0.000173 : 7
2008-10-04 15:12:44 : 0.000173 : 8
2008-10-04 15:20:58 : 0.000048 : 12
2008-10-04 15:33:45 : 0.000043 : 5
2008-10-04 15:38:45 : 0.000088 : 5

# cable snap
print datetime.date(1912,5,27) - datetime.date(1911,1,8)

505 8.5 minutes

# housing herniated
print datetime.date(1915,8,2) - datetime.date(1914,2,24)

524 8.5 minutes

# lunch
print datetime.date(1937,3,25) - datetime.date(1934,6,5)

1024 - 17 minutes

# break
print datetime.date(1958,2,28) - datetime.date(1956,4,8)

691 11.5 minutes

"""
